# -*- coding: utf-8 -*-
# @Time    : 2024/12/18 下午9:32
# @Author  : wjx
# @Email   : kanxinxin@163.com
# @File    : run.py
# @Software: PyCharm
"""
执行模块
"""

import pytest

from common import log
from common import shell
from config import config
from common import mail
from common import consts
from common.tools import zip_dir

if __name__ == '__main__':
    conf = config.Config()
    log = log.Log.get_logger()
    log.info('初始化配置文件,path=' + conf.log_path)

    shell = shell.Shell()
    xml_report_path = consts.XML_REPORT_PATH
    html_report_path = consts.HTML_REPORT_PATH
    cases_path = consts.CASE_PATH
    attach_file = 'report.zip'

    # 定义测试集
    args = ['-s', '-q', '--alluredir', xml_report_path, cases_path]
    pytest.main(args)

    # 生成页面报告到指定文件夹下，以及相关的依赖文件
    cmd = 'allure generate {} -o {}'.format(xml_report_path, html_report_path)

    try:
        shell.execute(cmd)
        # 压缩report文件夹
        zip_dir(consts.REPORT_PATH, attach_file)
    except Exception:
        log.error('执行用例失败，请检查环境配置')
        raise

    try:
        mail = mail.send_mail(attach_file)
    except Exception as e:
        log.error('发送邮件失败，请检查邮件配置')
        raise
